//
//  AddYunDanTwoVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/17.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "AddYunDanTwoVC.h"
#import "AddYunDanStatusView.h"
#import "AddYunDanTwoCell.h"
#import "AddYunDanEditGoodVC.h"
#import "AddYunDanThreeVC.h"

@interface AddYunDanTwoVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) AddYunDanStatusView *statusView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation AddYunDanTwoVC

-(AddYunDanStatusView *)statusView {
    if (!_statusView) {
        _statusView = [[AddYunDanStatusView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 75)];
        _statusView.status = 2;
    }
    return _statusView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *lastBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 60 - 15) * 0.5, 44)];
        [lastBtn setBackgroundColor:HexColor(0xffffff)];
        lastBtn.layer.masksToBounds = YES;
        lastBtn.layer.cornerRadius = 6;
        lastBtn.layer.borderWidth = 1;
        lastBtn.layer.borderColor = HexColor(0xF2F2F5).CGColor;
        [lastBtn setTitle:@"上一步" forState:0];
        [lastBtn setTitleColor:HexColor(0x585974) forState:0];
        lastBtn.titleLabel.font = BoldFontOfSize(16);
        [lastBtn addTarget:self action:@selector(lastClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:lastBtn];
        
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(lastBtn.nim_right + 15, 10, (_bottomView.nim_width - 60 - 15) * 0.5, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"下一步" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

- (void)lastClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

- (void)nextClick {
    __block BOOL canNext = NO;
    [self.addModel.selectCompany.selectCangKu.tiHuoBillArray enumerateObjectsUsingBlock:^(AddYunDanTiHuoBillModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.selected) {
            canNext = YES;
        }
    }];
    if (canNext) {
        AddYunDanThreeVC *vc = [AddYunDanThreeVC new];
        vc.addModel = self.addModel;
        if (self.wa_id.length) {
            vc.wa_id = self.wa_id;
        }
        [self.rt_navigationController pushViewController:vc animated:YES];
    }
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.statusView.nim_bottom, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight - self.statusView.nim_height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建运单(中转)";
    [self.view addSubview:self.statusView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    if (self.wa_id.length) {
        [HttpRequest PostWithApi:ReceiptDeliveryEditYunDanTiDanList params:@{@"wa_id":self.wa_id,@"w_id":self.addModel.selectCompany.selectCangKu.w_id} progress:^(NSProgress * _Nonnull progress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                self.addModel.selectCompany.selectCangKu.tiHuoBillArray = [AddYunDanTiHuoBillModel mj_objectArrayWithKeyValuesArray:responseObject];
                [self.tableView reloadData];
            }
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }else {
        [HttpRequest PostWithApi:ReceiptDeliveryAddYunDanTiDanList params:@{@"t_id":self.addModel.selectCompany.Id,@"plw_id":self.addModel.selectCompany.selectCangKu.w_id} progress:^(NSProgress * _Nonnull progress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                self.addModel.selectCompany.selectCangKu.tiHuoBillArray = [AddYunDanTiHuoBillModel mj_objectArrayWithKeyValuesArray:responseObject];
                [self.tableView reloadData];
            }
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.addModel.selectCompany.selectCangKu.tiHuoBillArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanTwoCell *cell = [AddYunDanTwoCell AddYunDanTwoCellWithTableView:tableView];
    AddYunDanTiHuoBillModel *model = self.addModel.selectCompany.selectCangKu.tiHuoBillArray[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.editBlock = ^{
        AddYunDanEditGoodVC *vc = [AddYunDanEditGoodVC new];
        vc.billModel = model;
        vc.finishBlock = ^{
            __block BOOL isSelected = NO;
            [model.list enumerateObjectsUsingBlock:^(AddYunDanTiHuoGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.selected) {
                    isSelected = YES;
                }
            }];
            model.selected = isSelected;
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        };
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    cell.selectBlock = ^{
        if (model.selected) {
            model.selected = NO;
        }else {
            __block BOOL isSelected = NO;
            [model.list enumerateObjectsUsingBlock:^(AddYunDanTiHuoGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.selected) {
                    isSelected = YES;
                }
            }];
            if (isSelected) {
                model.selected = isSelected;
            }else {
                [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请编辑提货清单"];
            }
        }
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanTwoCell *cell = [AddYunDanTwoCell AddYunDanTwoCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.nim_width, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
